package com.iamgod.savemoney.utils;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author lihui
 * �ַ�����
 *
 */
public class StringUtil {
	
	/**
	 * 取字符串前N位
	 * @param str  Դ�ַ�
	 * @param num  ��ȡ��
	 * @return
	 */
	public static String subString(String str,int num){
		if(str==null||str.equals("")){
			return "";
		}else{
			int len = str.length();
			if(len<num){
				num = len;
			}
			return str.substring(0, num);
		}
	}
	
	
	/**
	 * �ַ�null��"null"ת���ַ�
	 * �������ֵΪnull��תΪ""
	 * @param str
	 * @return
	 */
	public static String getStrNoNULL(String str){
		if(str==null||str.equals("null")){
			return "";
		}else{
			return str;
		}
	}
	
	
	/**
	 * URL���Ĳ������תutf-8
	 * @param str
	 * @return
	 */
	public static String setEnCodeByUTF8(String str){
		try {
			return java.net.URLEncoder.encode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
		
	}
	
	
	/**
	 * ��ɶ�����
	 * @param type ����  ֱͨ�εĶ�������� type="id"
	 * @return
	 */
	public static String createOrderId(String type){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date();
		String id = "";
		if(type==null){
			type = "id";
		}
		if(type.equals("id")){
			id = "10"+formatter.format(date)+String.valueOf(((int)(Math.random()*100000))+10000).substring(0, 4);
		}else{
			id = formatter.format(date)+String.valueOf(((int)(Math.random()*100000))+10000).substring(0, 4);
		}
		return id;
	}

	/**
	 * ��json��ʽstarLevelת����starlevel
	 * @param star json��ʽstarLeve
	 * @return starLevel�ַ�
	 * @author whzuo
	 */
	public static String getStarLevel(String star){
		if(star!=null){
			star = star.replace("{", "");
			star = star.replace("}", "");
			star = star.replace("starLevelEnum", "");
			star = star.replace("1", "");
			star = star.replace("2", "");
			star = star.replace("3", "");
			star = star.replace("4", "");
			star = star.replace("5", "");
			star = star.replace("6", "");
			star = star.replace(":", "");
			star = star.replace("\"", "");
		}
		
		return star;
	}
	
	/**
	 * ���32λ��Ψһ���
	 * 
	 * @return
	 */
	public static synchronized String getSequence() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}
}
